home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 15412 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.2 KB

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.infosystems.www.servers.unix,comp.lang.c
  4. Subject: Re: Opening a binary file.... (dealing with content-type & WWW)
  5. Date: Thu, 18 Apr 96 20:25:54 GMT
  6. Organization: none
  7. Message-ID: <829859154snz@genesis.demon.co.uk>
  8. References: <317351DE.E7E@draper.com>
  9. Reply-To: fred@genesis.demon.co.uk
  10. X-NNTP-Posting-Host: genesis.demon.co.uk
  11. X-Newsreader: Demon Internet Simple News v1.27
  12. X-Mail2News-Path: genesis.demon.co.uk
  13.  
  14. In article <317351DE.E7E@draper.com>
  15.            bshalton@draper.com "Brandon Shalton" writes:
  16.  
  17. >I have decided to take out the fork/exec and replace it with a 
  18. >simple open file, read contents, send to browser routine.
  19. >
  20. >This is my first cut...
  21. >
  22. >
  23. >
  24. >                fd2=open(entries[0].val,O_RDONLY);
  25. >                for(;;)
  26. >                        {
  27. >                if(     read(fd2, tbuf, 1) <=0 ) exit(1);
  28. >                        printf("%s", tbuf);
  29. >                        }
  30.  
  31. What is tbuf?
  32.  
  33. It is best to talk about things like open() and read() in
  34. a newsgroup specific to your system since they are not part
  35. of the C language.
  36.  
  37. Assuming that tbuf is an array of char then the read reads a single char
  38. into it (very inefficient) then you try to print the contents of the buffer
  39. with printf. The %s conversion specifier requires a pointer to a string as
  40. the corresponding argument. You haven't created a string (a null character
  41. terminated sequence of characters) here so subject to code elsewhere this
  42. is illegal.
  43.  
  44. >This snippet does read the file, and sends the file out..but when 
  45. >viewed from Microsoft Word, the file comes through as text, showing 
  46. >the control characters at the header of the file.  It looks like 
  47. >ascii is being sent out rather than binary.
  48. >
  49. >I have also tried:
  50. >
  51. >
  52. >
  53. >                                                                               
  54. >                                                 fin2=fopen(entries[0].val,
  55. >"rb");
  56. >                while( ch=fgetc(fin2) != EOF )
  57. >                        printf("%c", ch);
  58. >                exit(1);
  59.  
  60. That's better and just uses standard C library functions but there are a
  61. couple of problems with it. Firstly != binds more tightly than = so the
  62. while line is equivalent to:
  63.  
  64. >                while( ch = (fgetc(fin2) != EOF) )
  65.  
  66. whereas what you want is:
  67.  
  68. >                while((ch = fgetc(fin2)) != EOF)
  69.  
  70. You can also use getc() instead of fgetc() as it is likely to be the more
  71. efficient of the two.
  72.  
  73.     printf("%c", ch);
  74.  
  75. is a long-winded way of writing:
  76.  
  77.     putchar(ch);
  78.  
  79. Always show your variable declarations - in this case make sure that ch is
  80. an int so that it can deal with EOF correctly.
  81.  
  82. By default stdin,stdout and stderr are opened in text mode. Standard C
  83. provides no method to change this although your compiler may provide a
  84. system-specific method. On Unix systems there is no difference between
  85. binary and text modes but on many other systems (such as the ones on
  86. which Microsoft Word is most commonly found) there is a difference.
  87.  
  88. -- 
  89. -----------------------------------------
  90. Lawrence Kirby | fred@genesis.demon.co.uk
  91. Wilts, England | 70734.126@compuserve.com
  92. -----------------------------------------
  93.